<template>
  <div>
    <el-form class="input-form"
      label-position="right"
      label-width="120px"
      ref="editForm"
      :rules="propRules"
      :disabled="propReadonly"
      :model="form"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="品名" prop="matrName">
            <matr-select
              v-model="form.matrId"
              :propRows="matrs"
              :propText="propCurrentRow.matrName"
              :propDisabled="propReadonly"
              :propColumns="selectMatrColumns"
              :propSupplierId="propInitData.supplierId"
              :propOmoc="propInitData.flagOmoc"
              :propConsignment="propInitData.flagConsignment"
              propStyle="width:60vw"
              ref="matrGrid"
              @select="handleMatrSelect"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规格" prop="matrSpec">
            <el-input v-model="form.matrSpec" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="厂商" prop="mfgName">
            <el-input v-model="form.mfgName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="批次" prop="lot">
            <el-input v-model="form.lot" />
          </el-form-item>
        </el-col>        
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
           <el-form-item label="包装单位" prop="packageUnit">
            <matr-unit-select
              ref="matrUnitGrid"
              v-model="form.packageUnit"
              propUrl="/base/matrUnit/list"
              :propText="propCurrentRow.packageUnit"
              @select="handlePackageUnitChange"
              :propMatrId="form.matrId"
              :propPurchase="true"
              propValueField="unit"
              :propDisabled="strings.isBlank(form.matrId)"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数量" prop="packageQty">
            <el-input v-model="form.packageQty" @input="packageQtyChange" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="注册证号" prop="certificateNo">
            <el-input v-model="form.certificateNo" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="小计(元)" prop="subtotalAmount">
            <el-input v-model="form.subtotalAmount" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="结算科室" prop="deptName">
            <el-input v-model="form.deptName" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="form.remark" :autosize="{ minRows: 1, maxRows: 4}"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import MatrSelect from '@/components/base/MatrSelect'
import MatrUnitSelect from '@/components/base/MatrUnitSelect'

export default {
  data() {
    return {
      form: {},
      viewData: {},
      matrs: [],
      selectMatrColumns: [
        { label: '品名', name: 'name', width: 200, search: true },
        { label: '品规',  name: 'spec', width: 150, search: true },
        { label: '厂商名称',  name: 'mfgName', search: true },
        { label: '单品单位',  name: 'skuUnit', width: 80, search: false },
        { label: '单品单价',  name: 'skuPrice', width: 80, search: false }
      ]
    }
  },
  methods: { 
    //选择耗材事件
    handleMatrSelect(item) {
      let {id, name, spec, flagConsignment, flagOmoc, skuPrice, skuUnit, mfgName} = item
      this.$set(this.form, 'matrId', id)
      this.$set(this.form, 'matrName', name)
      this.$set(this.form, 'matrSpec', spec)
      this.$set(this.form, 'skuPrice', skuPrice)
      this.$set(this.form, 'skuUnit', skuUnit)
      this.$set(this.form, 'mfgName', mfgName)
    },
    //耗材单位变更时，重新计算换算比
    handlePackageUnitChange(item) {
      item && this.$set(this.form, 'packageRate', item.rate) 
      && this.$set(this.form, 'skuQty', this.form.packageQty * this.form.rate)
      && this.$set(this.form, 'subtotalAmount', this.form.skuPrice * this.form.skuQty)
    },
    packageQtyChange(value) {
      this.$set(this.form, 'skuQty', value * this.form.rate)
      this.$set(this.form, 'subtotalAmount', this.form.skuPrice * this.form.skuQty)
    }
  },
  created() {
    this.$emit('event-register', {
      validate: (callback) => {
        this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)

        // 设置switch 元素默认值
      },
      afterSave: () => {  },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {
      },
      beforeClose: () => {
        return true
      }
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object
  },
  components: {
    MatrSelect, MatrUnitSelect
  },
  computed: {
  }
}
</script>

<style lang="scss" scoped>
  .container {
    width: 100%;
    height: 100%;
  }
</style>
